function init(){
	imgPreview();
    submitUpdateForm('.btn-submit-update', evtAfterUpdateRaoVat);
	changeRaoVatImage();
    uploadImage($('#frm-upload-img'), getUploadedImage);
    doCropImage(evtAfterCropImage);
    changeRaoVatState();
}

function evtAfterUpdateRaoVat(data){
	if(document.URL != data.href){
        window.location = data.href;
    }
}

function changeRaoVatImage(){
    $(document.body).on('click', '.raovat-img-block', function(e) {
        e.preventDefault();
        var folder = $(this).attr('data-folder');
        var file_name = $(this).attr('data-name');
        var file_path = $(this).attr('href');
        var file_path_thumb = $(this).find('img.media-object').attr('src');

        $('#input-raovat-image').val(folder + file_name);
        $('#a-input-raovat-image').attr('href', file_path);
        $('#a-input-raovat-image img').removeAttr('data-src').removeAttr('style').attr('src', file_path_thumb);
        $('#modal-change-image').modal('hide');
        return false;
    });
}

function evtAfterCropImage(data){
    var newImage = $('#raovat-temp-block').clone().removeAttr('id').removeClass('hidden');
    newImage.find('a.thumbnail').attr('data-name', data.file_name);
    newImage.find('img.media-object').attr('src', data.file_path);
    $('ul#image-temp-upload').append(newImage);
    $('#input-crop-path').val('');
    $('.image-current').removeClass('hidden');
    $('.image-resize').addClass('hidden');
}

function changeRaoVatState(){
    $('input.input-raovat-active-flg, input.input-raovat-delete-flg').change(function(){
        var url = $(this).parents('table').attr('data-href');
        var value = $(this).is(':checked')==true?1:0;
        var name = $(this).attr('data-name');
        var raovat = {id: $(this).parents('tr').attr('data-id')};
        raovat[name] = value;
        changeState(url, {raovat: raovat});
    });
}